package appinstallationreceiver.com.login;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private EditText et_username;
    private EditText et_password;
    private CheckBox cb_isSave;
    private SharedPreferences sp ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);

        et_username = (EditText) findViewById(R.id.et_username);
        et_password = (EditText) findViewById(R.id.et_password);
        cb_isSave = (CheckBox) findViewById(R.id.cb_isSave);

        sp = getSharedPreferences("info",MODE_PRIVATE);
        boolean isSave = sp.getBoolean("isSave",false);
        if(isSave) {
            et_username.setText(sp.getString("username",""));
            et_password.setText(sp.getString("password",""));
            cb_isSave.setChecked(true);
        }
    }

    public void login(View v) {
        String username = et_username.getText().toString().trim();
        String password = et_password.getText().toString().trim();
        if(TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
            Toast.makeText(getApplicationContext(),"用户名或密码不能为空！！",Toast.LENGTH_SHORT).show();
        } else {
            boolean checked = cb_isSave.isChecked();
            SharedPreferences.Editor editor = sp.edit();
            if (checked) {
                editor.putString("username",username);
                editor.putString("password",password);
            }
            editor.putBoolean("isSave",checked);
            editor.commit();
        }
        //  处理登陆的逻辑
//        Intent intent = new Intent(getApplicationContext(),LoginActivity.class);
//        startActivity(intent);
    }

    public void register(View v){
        Intent intent = new Intent(getApplicationContext(),RegisterActivity.class);
        startActivity(intent);
    }
}
